<?php


namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Order;
use App\Customers;
use App\Info;
use DB;
class OrderController extends Controller{
        public function orderinfo($oid=1){

                // dd(DB::table('orders'));
                $ord=DB::table('orders')->where('order_id',$oid)->get();
                $yfk=DB::table('orders')->where('status',1)->get();
                // dd($ord);

                return view('admin/orderinfo',['ord'=>$ord,'yfk'=>$yfk]);
        }

        public function orderlist(){

                $ord=DB::table('orders')->get();
                return view('admin/orderlist',['ord'=>$ord]);


        }

    //status=为0则表示没有这个订单
    //为1表示未付款订单
    //为2表示已支付未发货订单
    //为3表示已发货订单
    //为4表示已完成订单(确认收货)
    //未付订单
    public function weifu(){
        $orders = Order::join('Customers','customers.uid','=','orders.uid')->where('orders.status','=','0')->select(['orders.*','customers.name'])->get();
        $infos = [];
        //dd($orders);
        foreach($orders as $v){
            $infos[] = Info::join('goods','goods.gid','=','infos.gid')->where(['infos.oid'=>$v->order_id])->get();
        }
        //dd($infos);
        return view('order/orderlistwf',['order'=>$orders,'info'=>$infos]);
    }

    //已付款未发货
    public function yishou(){
        $orders = Order::join('Customers','customers.uid','=','orders.uid')->where('orders.status','=','1')->select(['orders.*','customers.name'])->get();
        $infos = [];
        //dd($orders);
        foreach($orders as $v){
            $infos[] = Info::join('goods','goods.gid','=','infos.gid')->where(['infos.oid'=>$v->order_id])->get();
        }
        //dd($infos);
        return view('order/orderlistys',['order'=>$orders,'info'=>$infos]);
    }

    //已发货订单列表
    public function yifa(){
        $orders = Order::join('Customers','customers.uid','=','orders.uid')->where('orders.status','=','2')->select(['orders.*','customers.name'])->get();
        $infos = [];
        //dd($orders);
        foreach($orders as $v){
            $infos[] = Info::join('goods','goods.gid','=','infos.gid')->where(['infos.oid'=>$v->order_id])->get();
        }
        //dd($infos);
        return view('order/orderlistyf',['order'=>$orders,'info'=>$infos]);
    }

    //已完成订单
    public function end(){
        $orders = Order::join('Customers','customers.uid','=','orders.uid')->where('orders.status','=','3')->select(['orders.*','customers.name'])->get();
        return view('order/orderlistend',['order'=>$orders]);
    }

    //执行ajax发货操作
    public function fahuo(Request $req){
            $order = Order::find($req->order_id);
            $order->kuaididh = $req->kuaididh;
            $order->kuaidigs = $req->kd;
            $order->status = 3;
            $order->fhtime = time();
            $rs = $order->save();
            if ($rs) {
                    echo 1;
            } else {
                    echo 0;
            }
    }
    //删除订单
    public function del($oid){
        $rs = Order::find($oid);
        $rs = $rs->delete();
        if($rs){
            return redirect('order/wf');
        }else{
            return "删除失败";
        }
    }

    //订单详情页
    public function info($oid){
        $order = Order::find($oid);
        $user = Customers::find($order->uid);
        $goods = Info::join('goods','goods.gid','=','infos.gid')->where('infos.oid','=',$oid)->select(['infos.*','goods.goods_name',DB::raw('infos.curr_price*infos.num as infoprice')])->get();
        //dd($goods);
        return view('order/orderinfo',['order'=>$order,'user'=>$user,'goods'=>$goods]);
    }
}
